/**
 * Apr 29, 2010
 */
package org.tomale.id.inventory.management.ui;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.tomale.id.ProviderException;
import org.tomale.id.inventory.Unit;
import org.tomale.id.inventory.management.Activator;
import org.tomale.id.inventory.management.IInventoryProvider;
import org.tomale.id.inventory.management.InventoryManager;

/**
 * @author ferd
 *
 */
public class ItemGroupOverviewPage extends FormPage {

	public static final String PAGE_ID = "org.tomale.id.inventory.management.editor.itemgroup.overview";
	public static final String PAGE_TITLE = "Overview";

	IInventoryProvider _provider;
	
	Text _name;
	Text _desc;
	Combo _unit;

	ItemGroupEditorInput _input;
	
	public ItemGroupOverviewPage(FormEditor editor){
		super(editor,PAGE_ID,PAGE_TITLE);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		_input = (ItemGroupEditorInput) getEditorInput();
		
		final ScrolledForm form = managedForm.getForm();
		FormToolkit tk = managedForm.getToolkit();
		Composite body = form.getBody();
		tk.paintBordersFor(body);
		form.setText(this.getTitle());
		
		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 2;
		body.setLayout(layout);
		
		createGeneralSection(body, tk);
	}

	private void createGeneralSection(Composite parent, FormToolkit toolkit){
		Section section = toolkit.createSection(parent,
				Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
		section.setText("General Information");
		section.setDescription("This section describes general information about this item.");
		TableWrapData td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		
		Composite comp = toolkit.createComposite(section);
		toolkit.paintBordersFor(comp);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);

		Label l;

		// name
		l = toolkit.createLabel(comp, "Name");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_name = toolkit.createText(comp, _input.getItemGroupName());
		_name.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		// description
		l = toolkit.createLabel(comp, "Description");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_desc = toolkit.createText(comp, _input.getItemGroupDescription());
		_desc.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
	
		// unit
		l = toolkit.createLabel(comp, "Unit");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_unit = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(_unit);
		_unit.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		initUnits();
		
		section.setClient(comp);
	}
	
	private IInventoryProvider getProvider(){
		if(_provider == null){
			try {
				_provider = InventoryManager.getInstance().getProvider();
			}catch(ProviderException e){
				Activator.logError(e.getMessage());
			}
		}
		return _provider;
	}
	
	private void initUnits(){
		IInventoryProvider provider = getProvider();
		if(provider != null){
			try {
				List<Unit> units = provider.getUnits();
				for(Unit unit : units){
					_unit.add(unit.getAbbreviation());
					_unit.setData(unit.getAbbreviation(),unit);
				}
			}catch(ProviderException e){
				Activator.logError(e.getMessage());
			}
		} else {
			Activator.logError("Unable to obtain inventory provider.");
		}
	}
}
